import React, { useState, useEffect } from 'react'

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min)
  max = Math.floor(max)
  return Math.floor(Math.random() * (max - min + 1)) + min //含最大值，含最小值
}

export default function Test() {
  const [count, setCount] = useState(0)

  // useEffect 外层函数模拟了类组件中的componentDidMount和componentDidUpdate
  useEffect(() => {
    console.log('test组件挂载/更新了')
    // 内部函数模拟类组件中componentWillUnmount
    return () => {
      console.log('test组件即将卸载')
    }
  }, [])

  function handle() {
    setCount(getRandomIntInclusive(1, 2))
  }
  return (
    <div>
      {count}
      <button onClick={handle}>+</button>
    </div>
  )
}
